//
//  CYTransferBankCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/10.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTransferBankCell.h"
@interface CYTransferBankCell()
@property (nonatomic, strong) CYImageView *bankLogoIMG;//银行logo
@property (nonatomic, strong) CYLabel *bankLBL;//银行名称
@property (nonatomic, strong) CYLabel *addressLBL;//银行分行
@property (nonatomic, strong) CYLabel *accountLBL;//银行账号
@property (nonatomic, strong) CYLabel *nameLBL;//开户主的名称
@property (nonatomic, strong) CYImageView *selectIMG;//选中状态的图片
@property (nonatomic, strong) CYView *sectionLine;
@end
@implementation CYTransferBankCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH, AL(PT(160)));
        self.line.hidden = YES;
        self.sectionLine.hidden = NO;
        self.bankLogo = nil;
        [self setupDottedLine];//设置虚线
    }
    return  self;
}

- (CYImageView *)bankLogoIMG{
    if (!_bankLogoIMG) {
        _bankLogoIMG =[[CYImageView alloc]initWithImage:_bankLogo size:CGSizeMake(self.bounds.size.height - _sectionLine.bounds.size.height - AL(PT(80)),
                                                                                  self.bounds.size.height - _sectionLine.bounds.size.height - AL(PT(80)))
                                               isRadius:YES];
        _bankLogoIMG.loadingView.hidden = NO;
        [self addSubview:_bankLogoIMG];
        
        [_bankLogoIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AL(PT(20)));
            make.centerY.equalTo(self).with.offset(- _sectionLine.bounds.size.height / 2.0);
            make.size.mas_equalTo(_bankLogoIMG.bounds.size);
        }];
    }
    return _bankLogoIMG;
}

- (CYLabel *)bankLBL{
    if (!_bankLBL) {
        _bankLBL = [[CYLabel alloc] initWithText:_bankName size:AL(PT(30)) textcolor:ColorHex(0x333333)];
        [self addSubview:_bankLBL];
        
        [_bankLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bankLogoIMG.mas_right).with.offset(AL(PT(15)));
            make.top.equalTo(_bankLogoIMG.mas_top).with.offset(-AL(PT(10)));
            make.size.mas_equalTo(_bankLBL.bounds.size);
        }];
    }
    return _bankLBL;
}

- (CYLabel *)addressLBL{
    if (!_addressLBL) {
        _addressLBL = [[CYLabel alloc]initWithText:_address size:AL(PT(22)) textcolor:ColorHex(0xa9a9a9)];
        [self addSubview:_addressLBL];
        
        [_addressLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bankLBL.mas_right).with.offset(AL(PT(15)));
            make.centerY.equalTo(_bankLBL);
            make.size.mas_equalTo(_addressLBL.bounds.size);
        }];
    }
    return _addressLBL;
}

- (CYLabel *)accountLBL{
    if (!_accountLBL) {
        _accountLBL = [[CYLabel alloc]initWithText:_account size:AL(PT(26)) textcolor:ColorHex(0x333333)];
        [self addSubview:_accountLBL];
        
        [_accountLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bankLBL);
            make.bottom.equalTo(_bankLogoIMG.mas_bottom).with.offset( AL(PT(10)));
            make.size.mas_equalTo(_accountLBL.bounds.size);
        }];
    }
    return _accountLBL;
}

- (CYLabel *)nameLBL{
    if (!_nameLBL) {
        _nameLBL = [[CYLabel alloc]initWithText:_name size:AL(PT(22)) textcolor:ColorHex(0x333333)];
        [self addSubview:_nameLBL];
        
        [_nameLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).with.offset(- AL(PT(100 + 20)));
            make.centerY.equalTo(self);
            make.size.mas_equalTo(_nameLBL.bounds.size);
        }];
    }
    return _nameLBL;
}

- (CYImageView *)selectIMG{
    if (!_selectIMG) {
        _selectIMG = [[CYImageView alloc]initWithImageName:_selectImageName size:CGSizeMake(AL(PT(35)), AL(PT(35))) isRadius:YES];
        [self addSubview:_selectIMG];
        
        [_selectIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self.mas_right).with.offset(- AL(PT(100 / 2.0 - 35 / 2.0)));
            make.size.mas_equalTo(_selectIMG.bounds.size);
        }];
    }
    return _selectIMG;
}

- (CYView *)line{
    if (!_sectionLine) {
        _sectionLine = [[CYView alloc]initWithColor:ColorHex(0xf5f5f9)];
        _sectionLine.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH, AL(PT(12)));
        [self addSubview:_sectionLine];
        
        [_sectionLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).with.offset(0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(_sectionLine.bounds.size);
        }];
    }
    return _sectionLine;
}

- (void)setBankLogo:(UIImage *)bankLogo{
    if (_bankLogo != bankLogo)
        _bankLogo = bankLogo;
    self.bankLogoIMG.image = bankLogo;
}

- (void)setBankName:(NSString *)bankName{
    if (_bankName != bankName)
        _bankName = bankName;
    self.bankLBL.hidden = NO;
}

- (void)setAddress:(NSString *)address{
    if (_address != address)
        _address = address;
    self.addressLBL.hidden = NO;
}

- (void)setAccount:(NSString *)account{
    if (_account != account)
        _account = [NSString spaceBetweenFourCharacters:account];
    self.accountLBL.hidden = NO;
}

- (void)setName:(NSString *)name{
    if (_name != name)
        _name = name;
    self.nameLBL.hidden = NO;
    _selectImageName = @"unselect";
    self.selectIMG.hidden = NO;
    
}

- (void)setupDottedLine{
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:self.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) /2)];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:ColorHex(0xf5f5f9).CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:1];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:AL(4)], [NSNumber numberWithInt:AL(2)], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, SCREEN_WIDTH - AL(PT(100)), 0);
    CGPathAddLineToPoint(path, NULL,SCREEN_WIDTH - AL(PT(100)), AL(PT(170)));
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [self.layer addSublayer:shapeLayer];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    [super setSelected:selected animated:animated];
    _selectIMG.image = (selected)?[UIImage imageNamed:@"selected"]:[UIImage imageNamed:@"unselect"];

//    if (selected)
//        self.block(@{@"bank":_bankName,
//                     @"account":_account,
//                     @"address":_address,
//                     @"name":_name});
    
    // Configure the view for the selected state
}


@end
